package dtec.project.whereareu;


import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	//set up URL server
	public static final String URL="http://58.11.46.176:90";
	int  uid=0;
	UserData db;
	Button gos,bupdate;
	TextView tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bupdate=(Button)findViewById(R.id.sign);
		gos=(Button)findViewById(R.id.gosearch);
		tv=(TextView)findViewById(R.id.usertext);
		db=new UserData(this);
        ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            
        }
        else{
        	Toast.makeText(this, "not online", Toast.LENGTH_LONG).show();
        	finish();
        }

		//check login
		if(db.checkStatus()==false){
		    Intent intent = new Intent(MainActivity.this, Login.class);
		    finish();
		    startActivity(intent);
		}
		tv.setText("User id:"+db.getUserID()+" User name:"+db.getUsername());
		//funciont to go to page search
		gos.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this, List_user.class);
				finish();
				startActivity(intent);
			}});
		//function to go page update data
		bupdate.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			    Intent intent = new Intent(MainActivity.this, MainInterface.class);
			    finish();
			    startActivity(intent);
			}});	
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.menu_settings:
	        if(db.logoutUser(db.getUserID())){
	        	Intent intent=this.getIntent();
	        	finish();
	        	startActivity(intent);
	        }
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}

}
